package com.chery.media.ui.music

import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.utils.widget.ImageFilterView
import coil.load
import com.chery.media.R
import com.chery.media.contacts.EventContacts
import com.chery.media.contacts.MusicType
import com.chery.media.library.extend.clickNoRepeat
import com.chery.media.library.extend.loadPicture
import com.chery.media.library.extend.toTime
import com.chery.media.music.BluetoothMusicTool
import com.chery.media.ui.main.MainActivity
import com.chery.media.ui.play.playViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lzf.easyfloat.EasyFloat
import com.lzf.easyfloat.enums.ShowPattern

object MusicFloatWindow {

    fun showMusic(mainActivity: MainActivity){
        if (playViewModel.isPlaying().not()) {
            return
        }
        EasyFloat.with(mainActivity)
            .setTag("smallWindow")
            .setShowPattern(ShowPattern.BACKGROUND)
            .setGravity(Gravity.BOTTOM or Gravity.START, 20, 20)
            .setLayout(R.layout.float_music_window) { windowMusic ->
                val musicCover = windowMusic.findViewById<ImageFilterView>(R.id.musicCover)
                val musicNameText = windowMusic.findViewById<AppCompatTextView>(R.id.musicNameText)
                val musicAnchorNameText = windowMusic.findViewById<AppCompatTextView>(R.id.musicAnchorNameText)
                val playIcon = windowMusic.findViewById<ImageFilterView>(R.id.pauseOrPlay)
                musicNameText.text = playViewModel.currentPlayMusic()?.musicName
                musicAnchorNameText.text = playViewModel.currentPlayMusic()?.musicAuthor
                musicCover.loadPicture(playViewModel.currentPlayMusic()?.musicCover, R.drawable.ic_default_cover)
                playIcon.setImageResource(if (playViewModel.isPlaying()) R.drawable.svg_play_pause else R.drawable.svg_play)
                windowMusic.findViewById<View>(R.id.nextSong).clickNoRepeat {
                    playViewModel.playNext()
                }
                playIcon.clickNoRepeat {
                    playViewModel.continuePlayOrPause()
                }
                windowMusic.findViewById<View>(R.id.previousSong).clickNoRepeat {
                    playViewModel.playPrevious()
                }
                if (playViewModel.currentType.value == MusicType.BLUETOOTH) {
                    playViewModel.currentBluetoothPlayMusic.observe(mainActivity) {
                        if (it.isPlaying == true) {
                            playIcon.setImageResource(R.drawable.svg_play_pause)
                        } else {
                            playIcon.setImageResource(R.drawable.svg_play)
                        }
                        musicNameText.text = it.musicName
                        musicAnchorNameText.text = it.musicAuthor
                        musicCover.loadPicture(it.musicCover, R.drawable.ic_default_cover)
                    }
                } else if (playViewModel.currentType.value == MusicType.USB) {
                    playViewModel.currentUsbPlayMusic.observe(mainActivity) {
                        if (it.isPlaying == true) {
                            playIcon.setImageResource(R.drawable.svg_play_pause)
                        } else {
                            playIcon.setImageResource(R.drawable.svg_play)
                        }
                        musicNameText.text = it.musicName
                        musicAnchorNameText.text = it.musicAuthor
                        musicCover.loadPicture(it.musicCover, R.drawable.ic_default_cover)
                    }
                } else if (playViewModel.currentType.value == MusicType.HOT_LIST) {
                    playViewModel.currentHotPlayMusic.observe(mainActivity) {
                        if (it.isPlaying == true) {
                            playIcon.setImageResource(R.drawable.svg_play_pause)
                        } else {
                            playIcon.setImageResource(R.drawable.svg_play)
                        }
                        musicNameText.text = it.musicName
                        musicAnchorNameText.text = it.musicAuthor
                        musicCover.loadPicture(it.musicCover, R.drawable.ic_default_cover)
                    }
                }
            }.show()
//        if (musicViewModel.currentPlayMusic.value == null) {
//            return
//        }
//        EasyFloat.with(mainActivity)
//            .setTag("smallWindow")
//            .setShowPattern(ShowPattern.BACKGROUND)
//            .setGravity(Gravity.BOTTOM or Gravity.START, 20, 20)
//            .setLayout(R.layout.float_music_window) { windowMusic ->
//                val musicCover = windowMusic.findViewById<ImageFilterView>(R.id.musicCover)
//                val musicNameText = windowMusic.findViewById<AppCompatTextView>(R.id.musicNameText)
//                val musicAnchorNameText = windowMusic.findViewById<AppCompatTextView>(R.id.musicAnchorNameText)
//                val playIcon = windowMusic.findViewById<ImageFilterView>(R.id.pauseOrPlay)
//                musicNameText.text = musicViewModel.currentPlayMusic.value?.musicName
//                musicAnchorNameText.text = musicViewModel.currentPlayMusic.value?.musicAuthor
//                musicCover.loadPicture(musicViewModel.currentPlayMusic.value?.musicCover, R.drawable.ic_default_cover)
//                playIcon.setImageResource(if (musicViewModel.playStatus.get()) R.drawable.svg_play_pause else R.drawable.svg_play)
//                windowMusic.findViewById<View>(R.id.nextSong).clickNoRepeat {
//                    musicViewModel.playNext()
//                }
//                playIcon.clickNoRepeat {
//                    if (musicViewModel.playStatus.get()) {
//                        musicViewModel.playPause()
//                    } else {
//                        musicViewModel.playMusic(0)
//                    }
//                }
//                windowMusic.findViewById<View>(R.id.previousSong).clickNoRepeat {
//                    musicViewModel.playPrevious()
//                }
//                musicViewModel.currentPlayMusic.observe(mainActivity) {
//                    if (it.isPlaying == true) {
//                        playIcon.setImageResource(R.drawable.svg_play_pause)
//                        musicViewModel.playStatus.set(true)
//                    } else {
//                        playIcon.setImageResource(R.drawable.svg_play)
//                        musicViewModel.playStatus.set(false)
//                    }
//                    musicNameText.text = it.musicName
//                    musicAnchorNameText.text = it.musicAuthor
//                    musicCover.loadPicture(it.musicCover, R.drawable.ic_default_cover)
//                }
//            }.show()
    }

    fun dismissMusic(){
        EasyFloat.dismiss("smallWindow")
    }

}